﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace FU.SE0208.G1.Controller
{
    public class DiscountHandler
    {
        public bool addNew(FU.SE0208.G1.Entities.Discount obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("AddDiscount", conn);
                command.CommandType = CommandType.StoredProcedure;
                #region
                command.Parameters.Add(new SqlParameter("@ValidFrom", SqlDbType.DateTime)).Value = obj.ValidFrom1;
                command.Parameters.Add(new SqlParameter("@ValidTo", SqlDbType.DateTime)).Value = obj.ValidTo1;
                command.Parameters.Add(new SqlParameter("@ValidFrom", SqlDbType.DateTime)).Value = obj.ValidFrom1;
                command.Parameters.Add(new SqlParameter("@DisPercent", SqlDbType.Float)).Value = obj.DisPercent1;
                command.Parameters.Add(new SqlParameter("@ProID", SqlDbType.BigInt)).Value = Convert.ToInt64(obj.ProID1);
                #endregion
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool del(FU.SE0208.G1.Entities.Discount obj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("DeleteDiscount", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@DisID", SqlDbType.Int)).Value = Convert.ToInt32(obj.DisID1);
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public bool edit(FU.SE0208.G1.Entities.Discount obj, FU.SE0208.G1.Entities.Discount newObj)
        {
            SqlConnection conn = DbHandler.getConn();
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("EditDiscount", conn);
                command.CommandType = CommandType.StoredProcedure;
                #region
                command.Parameters.Add(new SqlParameter("@DisID", SqlDbType.DateTime)).Value = Convert.ToInt32(obj.DisID1);
                command.Parameters.Add(new SqlParameter("@ValidFrom", SqlDbType.DateTime)).Value = newObj.ValidFrom1;
                command.Parameters.Add(new SqlParameter("@ValidTo", SqlDbType.DateTime)).Value = newObj.ValidTo1;
                command.Parameters.Add(new SqlParameter("@ValidFrom", SqlDbType.DateTime)).Value = newObj.ValidFrom1;
                command.Parameters.Add(new SqlParameter("@DisPercent", SqlDbType.Float)).Value = newObj.DisPercent1;
                command.Parameters.Add(new SqlParameter("@ProID", SqlDbType.BigInt)).Value = Convert.ToInt64(newObj.ProID1);
                #endregion
                int count = command.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { return false; }
            finally { conn.Close(); conn.Dispose(); }
        }

        public DataSet getAll(string table)
        {
            DataSet ds = new DataSet("G1");
            DbHandler.getQuery(ds, "Select * from [" + table + "]", table);
            return ds;
        }
    }
}
